package com.hkj.configcenter.admin.filter;

import com.hkj.configcenter.admin.controller.LoginController;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();

        if (session.getAttribute(LoginController.LOGIN_NAME) == null) {
            PrintWriter printWriter = servletResponse.getWriter();
            printWriter.write("no login");

            HttpServletResponse response = (HttpServletResponse) servletResponse;
            response.sendRedirect("/");
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

}